package com.example.hospital_208.controller;

import com.example.hospital_208.po.WorkPlan;
import com.example.hospital_208.service.WorkPlanService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/work-plans")
@Tag(name = "排班计划管理接口", description = "医院排班计划的增删改查操作")
public class WorkPlanController {

    private final WorkPlanService workPlanService;

    public WorkPlanController(WorkPlanService workPlanService) {
        this.workPlanService = workPlanService;
    }

    // 获取所有排班计划
    @GetMapping
    @Operation(summary = "查询所有排班计划", description = "获取医院所有的排班计划信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(schema = @Schema(implementation = WorkPlan.class)))
    })
    public List<WorkPlan> getAllWorkPlans() {
        return workPlanService.listAllWorkPlans();
    }

    // 获取所有排班日期
    @GetMapping("/days")
    @Operation(summary = "获取所有排班日期", description = "获取系统中所有已排班的日期列表")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(schema = @Schema(type = "array", example = "2025-06-30")))
    })
    public List<String> getAllDays() {
        return workPlanService.listAllDays();
    }

    // 根据日期获取排班计划
    @GetMapping("/{year}/{month}/{day}")
    @Operation(summary = "根据日期查询排班计划", description = "通过年、月、日获取具体排班计划信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(schema = @Schema(implementation = WorkPlan.class))),
            @ApiResponse(responseCode = "404", description = "排班计划不存在")
    })
    public WorkPlan getWorkPlanByDate(
            @PathVariable("year")
            @Parameter(description = "年份", example = "2025", required = true)
            int year,

            @PathVariable("month")
            @Parameter(description = "月份", example = "06", required = true)
            int month,

            @PathVariable("day")
            @Parameter(description = "日期", example = "30", required = true)
            int day) {
        return workPlanService.getWorkPlanByDay(year, month, day);
    }

    // 根据具体日期字符串获取排班计划
    @GetMapping("/day/{day}")
    @Operation(summary = "根据日期字符串查询排班计划", description = "通过日期字符串(YYYY-MM-DD)获取具体排班计划信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(schema = @Schema(implementation = WorkPlan.class))),
            @ApiResponse(responseCode = "404", description = "排班计划不存在")
    })
    public WorkPlan getWorkPlanByDay(
            @PathVariable("day")
            @Parameter(description = "日期（格式：YYYY-MM-DD）", example = "2025-06-30", required = true)
            String day) {
        return workPlanService.getWorkPlanByDay(day);
    }

    // 新增排班计划
    @PostMapping
    @Operation(summary = "新增排班计划", description = "向系统中添加排班计划信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "添加成功",
                    content = @Content(schema = @Schema(type = "string"))),
            @ApiResponse(responseCode = "400", description = "参数错误或排班已存在")
    })
    public String addWorkPlan(@RequestBody
                              @Parameter(description = "排班计划实体对象",
                                      content = @Content(schema = @Schema(implementation = WorkPlan.class)))
                              WorkPlan workPlan) {
        int result = workPlanService.addWorkPlan(workPlan);
        return result > 0 ? "排班计划添加成功" : "排班计划添加失败";
    }

    // 更新排班计划状态
    @PutMapping("/status")
    @Operation(summary = "更新排班计划状态", description = "更新已有排班计划的状态")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "更新成功",
                    content = @Content(schema = @Schema(type = "string"))),
            @ApiResponse(responseCode = "400", description = "参数错误或排班不存在")
    })
    public String updateWorkPlanStatus(
            @RequestParam("day")
            @Parameter(description = "日期（格式：YYYY-MM-DD）", example = "2025-06-30", required = true)
            String day,

            @RequestParam("status")
            @Parameter(description = "状态值", example = "1", required = true)
            int status) {
        int result = workPlanService.updateWorkPlanStatus(day, status);
        return result > 0 ? "排班计划状态更新成功" : "排班计划状态更新失败";
    }

    // 根据周获取排班信息
    @GetMapping("/week/{week}")
    @Operation(summary = "根据周获取排班信息", description = "通过周范围获取排班信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(schema = @Schema(implementation = WorkPlan.class)))
    })
    public List<WorkPlan> getWorkPlanByWeek(
            @PathVariable("week")
            @Parameter(description = "周范围（格式：2025-06-24,2025-06-30）", example = "2025-06-24,2025-06-30", required = true)
            String week) {
        return workPlanService.getWorkPlanByWeek(week);
    }
}